package leetcode.BinarySearch;

import java.util.Arrays;

public class SimpleBinarySearch {

    public static int search(int[] nums, int target) {
        if(nums.length==1){
            if(nums[0]==target){
                return 0;
            }
        }
        int left = 0;
        int right = nums.length - 1;

        while (true) {
            int mid = (left + right) / 2;
            if (target > nums[mid]) {
                left = mid + 1;

            } else if (target < nums[mid]) {
                right = mid - 1;
            } else {
                return mid;
            }

            if (left > right) {
                break;
            }
        }
        return -1;

    }

    public static void main(String args[]) {
        int[] nums = {1, 2, 6,4, 5};
        int target = 61;
        long start = System.currentTimeMillis();
        System.out.println(search(nums, target));
        long end = System.currentTimeMillis();

        System.out.println("time = " + (end - start));

    }

}
